package com.newspublish.system.controller;


import com.newspublish.system.entity.system.Comment;
import com.newspublish.system.service.CommentService;
import com.newspublish.system.service.impl.CommentServiceImpl;
import com.newspublish.system.utils.result.Result;
import com.newspublish.system.utils.util.GetMassege;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "评论管理")
@RestController
@RequestMapping("/admin/news/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private CommentServiceImpl commentServiceImpl;

    @Autowired
    private GetMassege getMassege;

    @ApiOperation(value="获取所有评论")
    @GetMapping("/getAllComments")
    public Result getAllComments(){
        return commentService.getALLComments();
    }

    @ApiOperation(value="点赞评论")
    @PutMapping("/likeComment/{id}")
    public Result likeComment(@PathVariable("id") Long id){
        return commentService.likeComment(id);
    }

    @ApiOperation(value="根据id获取评论详情")
    @GetMapping("/detailComment/{id}")
    public Result selectDetailById(@PathVariable("id") Long id){
        return commentService.getDetailById(id);
    }

    //管理员模块

    @ApiOperation(value="管理员获取谋篇文章下的所以评论")
    @GetMapping("/getAllCOmmentOne/{pageNum}/{newsId}")
    public Result getAllCommentOne(@PathVariable("pageNum") Integer pageNum,
                                   @PathVariable("newsId") Long newsId){
        return commentService.getAllCommentOne(pageNum,newsId);
    }

    @ApiOperation(value="管理元获取子评论")
    @GetMapping("/getChildrenComment/parentId")
    public Result getChildrenComment(@PathVariable("parentId") Integer parentId){
        Comment comment =new Comment();
        comment.setCommentId(parentId);
        commentServiceImpl.deepSearchManager(comment);
        return Result.ok(comment);
    }

    @ApiOperation(value="管理员删除评论")
    @DeleteMapping("/deleteComment/{id}")
    public Result deleteCommentById(@PathVariable("id") Long id){
        return commentService.deleteCommentByAdmin(id);
    }

    @ApiOperation(value="添加一级评论")
    @PostMapping("/addCommentOne")
    public Result addCommentOne(Comment comment){
        return commentService.addComment(comment);
    }

    @ApiOperation(value="添加二级评论")
    @PostMapping("/addCommentTwo")
    public Result addCommentTTow(Comment comment){
        return commentService.addCommentLevel2(comment);
    }


    @ApiOperation(value="本人删除自己的评论")
    @DeleteMapping("/deleteCommentByI/{id}")
    public Result deleteCommentByI(@PathVariable("id") Long id){
        //获取当前登录者的用户名
        String user= getMassege.getUser();
        return commentService.deleteCommentByI(id,user);
    }

    @ApiOperation(value="普通用户获取子评论")
    @GetMapping("/commonGetCommemnts/{commentId}")
    public Result commonGetComments(@PathVariable("commentId") Integer commentId){
        Comment comment =new Comment();
        comment.setCommentId(commentId);
        commentServiceImpl.deepSearch(comment);
        return Result.ok(comment);
    }


}
